# coding=utf-8

import openpyxl
import xlrd

x1 = xlrd.open_workbook('lan_trans.xls')
#print 'sheet_names:', x1.sheet_names()
#print 'sheet_number:', x1.nsheets

# 打开头文件
print ('handle header file')
fileheader = open('languages.h', 'w', encoding='utf-8')
fileheader.write('#ifndef __LANGUAGES_H__\n')
fileheader.write('#define __LANGUAGES_H__\n\n')


fileheader.write('#ifdef __cplusplus\n')
fileheader.write('extern "C" {\n')
fileheader.write('#endif\n\n')


fileheader.write('#define LANS_MAX_CNT\t\t\t\t(%d)\n' % (2))
fileheader.write('#define LANS_DEF_IDX\t\t\t\t(%d)\n\n' % (1))


for sheet in x1.sheet_names():
	sheet1 = x1.sheet_by_name(sheet)
	index = 0
	for row in range(2, sheet1.nrows):
		index += 1
	fileheader.write('#define E_LAN_%s_MAX_CNT\t\t\t\t(%d)\n' % (sheet1.name.upper(), index))
fileheader.write('\n')

# 遍历填充枚举
for sheet in x1.sheet_names():
	sheet1 = x1.sheet_by_name(sheet)

	#print "sheet name:", sheet1.name   # get sheet name
	#print "row num:", sheet1.nrows  # get sheet all rows number
	#print "col num:", sheet1.ncols  # get sheet all columns number

	index = 0
	fileheader.write('\n/********************** %s start **************************/\n' % sheet1.name)
	fileheader.write('enum\n{\n')
	for row in range(2, sheet1.nrows):
		#print sheet1.cell_value(row, 0).encode('utf8')
		cell_value = sheet1.cell_value(row, 0)
		if len(cell_value) != 0:
			fileheader.write('\tE_LAN_%s_%s = %d,\n' % (sheet1.name.upper(), cell_value, index))
		else:
			fileheader.write('\tE_LAN_%s_%d = %d,\n' % (sheet1.name.upper(), index, index))
		index += 1
	fileheader.write('};')
	fileheader.write('\n/********************** %s end **************************/\n' % sheet1.name)

fileheader.write('\n')


for sheet in x1.sheet_names():
	sheet1 = x1.sheet_by_name(sheet)
	fileheader.write("extern const char *lan_%s_str[LANS_MAX_CNT][E_LAN_%s_MAX_CNT];\n" % (sheet1.name, sheet1.name.upper()))

fileheader.write('\n#ifdef __cplusplus\n')
fileheader.write('} /* extern "C" */\n')
fileheader.write('#endif\n\n')

fileheader.write('#endif//__LANGUAGES_H__\n')

# 打开源文件
print ('handle source file')
filesource = open('languages.c', 'w', encoding='utf-8')
filesource.write('#include "languages.h"\n')
filesource.write('\n')

# 遍历转化格式
for sheet in x1.sheet_names():
	#print '\n'
	sheet1 = x1.sheet_by_name(sheet)

#	print "sheet name:", sheet1.name   # get sheet name
#	print "row num:", sheet1.nrows  # get sheet all rows number
#	print "col num:", sheet1.ncols  # get sheet all columns number
	filesource.write('\n/********************** %s start **************************/' % sheet1.name)
	filesource.write("\nconst char *lan_%s_str[LANS_MAX_CNT][E_LAN_%s_MAX_CNT] = \n{" % (sheet1.name, sheet1.name.upper()))
	for col in range(1, sheet1.ncols):	
		filesource.write("\n\t{\t/**** %s start ****/\n" % (sheet1.cell_value(1, col)))
		for row in range(2, sheet1.nrows):
			#print sheet1.cell_value(row, col)
			filesource.write('\t\t"%s",\n' % (sheet1.cell_value(row, col)))
		filesource.write('\t},\t/**** %s end ****/' % (sheet1.cell_value(1, col)))
	filesource.write('\n};')
	filesource.write('\n/********************** %s end **************************/\n' % sheet1.name)

filesource.close()
print ('handle ok')